<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ artObj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="artObj.aut_name" :label="artObj.pubdate">
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              v-if="artObj.is_followed"
              type="info"
              size="mini"
              @click="clickFn(false)"
              >已关注</van-button
            >
            <van-button
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              @click="clickFn(true)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          v-if="artObj.attitude === 1"
          icon="good-job"
          type="danger"
          size="small"
          @click="likeFn(false)"
          >已点赞</van-button
        >
        <van-button
          v-else
          icon="good-job-o"
          type="danger"
          size="small"
          plain
          @click="likeFn(true)"
          >点赞</van-button
        >
      </div>
    </div>

    <!-- 文章评论区域 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="50"
      :immediate-check="false"
    >
      <comment-list
        v-for="item in commentLis"
        :key="item.com_id"
        :obj="item"
        :isShowCmtInput="isShowCmtInput"
      ></comment-list>
    </van-list>
    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="isShowCmtInput === true">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="isShowCmtInput = false">发表评论</div>
      <div class="icon-box">
        <van-badge :content="totalCount" max="99">
          <van-icon name="comment-o" size="0.53333334rem" @click="moveFn" />
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        v-focus
        v-model="comValue"
        @blur="blurFn"
        placeholder="友善评论、理性发言、阳光心灵"
      ></textarea>
      <van-button type="default" :disabled="comValue.length === 0" @click="send"
        >发布</van-button
      >
    </div>
  </div>
</template>

<script>
import {
  articleDetailAPI,
  followingslAPI,
  unfollowingslAPI,
  articleLikeAPI,
  articleDisLikeAPI,
  commentsAPI,
  sendCommentAPI
} from '@/api'
import CommentList from './CommentList.vue'
export default {
  components: { CommentList },
  name: 'ArticleDetail',
  data () {
    return {
      artObj: {}, // 文章对象
      commentLis: [], // 评论数据
      totalCount: 0, // 显示评论总数
      isShowCmtInput: true,
      loading: false, // 底部加载状态
      finished: false, // 底部是否加载完成
      offset: null, // 下一页(偏移id), 初始值null(第一页不需要传, axios遇到null忽略此参数)
      comValue: '' // 评论内容
    }
  },
  async created () {
    // 获取评论接口
    const res = await articleDetailAPI({
      id: this.$route.query.aid
    })
    console.log(res)
    this.artObj = res.data.data
  },
  methods: {
    // 封装评论接口
    async getCommentListFn () {
      // 评论
      const res = await commentsAPI({
        artId: this.$route.query.aid,
        // 将offset偏移量带给后台
        offset: this.offset
      })
      console.log(res)
      // 判断是否有数据
      if (res.data.data.results.length === 0) {
        this.finished = true // 加载完成
      }
      this.commentLis = [...this.commentLis, ...res.data.data.results] // 合并数据保存到变量中
      this.totalCount = res.data.data.total_count || '' // 无评论为0，不显示会标
      this.offset = res.data.data.last_id // 保存起来为了做分页
      // 关闭加载状态
      this.loading = false
    },
    // 关注与取关
    async clickFn (bool) {
      if (bool === true) {
        // 关注
        this.artObj.is_followed = true
        await followingslAPI({
          target: this.artObj.aut_id
        })
      } else {
        // 取关
        this.artObj.is_followed = false
        await unfollowingslAPI({
          uid: this.artObj.aut_id
        })
      }
    },
    // 点赞
    async likeFn (bool) {
      if (bool === true) {
        this.artObj.attitude = 1
        await articleLikeAPI({
          target: this.$route.query.aid
        })
      } else {
        this.artObj.attitude = -1
        await articleDisLikeAPI({
          target: this.$route.query.aid
        })
      }
    },
    // 点击评论按钮，滚动到底部
    async moveFn () {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    },
    // 发布评论-失去焦点
    blurFn () {
      setTimeout(() => {
        this.isShowCmtInput = true
      })
    },
    // 发布评论
    async send () {
      const res = await sendCommentAPI({
        artId: this.$route.query.aid, // 当前作者ID
        content: this.comValue // 评论内容
      })
      console.log(res)
      // 保存新评论对象到数组中
      this.commentLis = res.data.data.new_obj
      // 数量加1
      this.totalCount++
      // 清除输入框内容
      this.comValue = ''
    },
    // 加载更多
    onLoad () {
      this.getCommentListFn()
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
